package com.yaolong.api.modules.gen.utils;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.generator.IDatabaseQuery;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.IDbQuery;
import com.baomidou.mybatisplus.generator.config.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.querys.DB2Query;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.querys.OracleQuery;
import com.baomidou.mybatisplus.generator.config.querys.PostgreSqlQuery;
import com.baomidou.mybatisplus.generator.config.querys.SqlServerQuery;
import com.yaolong.api.configuration.properties.CodeGenProperties;
import com.yaolong.api.modules.gen.constants.GenConstants;
import com.yaolong.api.modules.gen.model.entity.DataSource;
import com.yaolong.api.utils.SpringUtil;
import com.yaolong.commons.exceptions.BusinessException;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author yaolong
 * @version V1.0.0
 * @program AgileFastFK3.0
 * @description mybatisPlush代码生成工具类
 * @create 2023-01-03 16:43
 **/
@Component
public class MybatisPlusCodeGenUtil {

    /**
     * <p>
     * 根据type获取IDbQuery
     * </p>
     * @version 1.0.0 <br>
     * @date 2023/1/3 16:45 <br>
     * @author yaolonga <br>
     * @param dbType
     * @return com.baomidou.mybatisplus.generator.config.IDbQuery
     */
    public static IDbQuery getDbQuery(String dbType) {
        //默认返回mysql
        IDbQuery query = new MySqlQuery();

        switch (dbType) {
            case GenConstants.DB_TYPE_MYSQL:
                query = new MySqlQuery();
                break;
            case GenConstants.DB_TYPE_ORACLE:
                query = new OracleQuery();
                break;
            case GenConstants.DB_TYPE_BD2:
                query = new DB2Query();
                break;
            case GenConstants.DB_TYPE_POSTGRESQL:
                query = new PostgreSqlQuery();
                break;
            case GenConstants.DB_TYPE_SQLSERVER:
                query = new SqlServerQuery();
                break;
            default:
                break;
        }
        return query;
    }


    /**
     * <p>
     * 获取表结构信息
     * </p>
     * @version 1.0.0 <br>
     * @date 2023/1/3 16:50 <br>
     * @author yaolonga <br>
     * @param dataSource
     * @return java.util.List<com.baomidou.mybatisplus.generator.config.po.TableInfo>
     */
    public static List<TableInfo> getTableInfoList(DataSource dataSource,String ...prefix){
        try {
            DataSourceConfig.Builder builder = new DataSourceConfig.Builder(dataSource.getDbUrl(), dataSource.getDbUserName(), dataSource.getDbPassword());
            builder.dbQuery(MybatisPlusCodeGenUtil.getDbQuery(dataSource.getDbType()));
            DataSourceConfig dataSourceConfig = builder
                    .build();

            StrategyConfig.Builder builder1 = new StrategyConfig.Builder().enableSchema();
            if (ObjectUtil.isNotEmpty(prefix)){
                builder1.addTablePrefix(prefix);
            }

            //获取代码生成配置
            CodeGenProperties properties = SpringUtil.getBean(CodeGenProperties.class);
            StrategyConfig strategyConfig = builder1.entityBuilder().addSuperEntityColumns(properties.getSuperEntityColumns()).build();

            ConfigBuilder configBuilder = new ConfigBuilder(new PackageConfig.Builder().build(),dataSourceConfig,strategyConfig,new TemplateConfig.Builder().build(),new GlobalConfig.Builder().build(),new InjectionConfig.Builder().build());
            IDatabaseQuery.DefaultDatabaseQuery defaultDatabaseQuery = new IDatabaseQuery.DefaultDatabaseQuery(configBuilder);
            return defaultDatabaseQuery.queryTables();
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException(e.getMessage());
        }
    }
}
